﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XSTDZ.ERP.Application.DataContracts;
using AutoMapper;
using XSTDZ.ERP.Domain.Models;
using XSTDZ.ERP.Core.Repositories;
using XSTDZ.ERP.Core.Generator;

namespace XSTDZ.ERP.Application.Implements
{
    public class OrgServiceImpl:ApplicationServiceBase,IOrgService
    {
        IRepository<Org> _orgRepository;
        IRepository<Dept> _deptRepository;
        public OrgServiceImpl(IRepositoryContext repositoryContext, IRepository<Org> orgRepository, IRepository<Dept> deptRepository)
            : base(repositoryContext)
        {
            this._orgRepository = orgRepository;
            this._deptRepository = deptRepository;
        }
        public Guid AddOrg(OrgDto orgDto)
        {
            var org = Mapper.Map<OrgDto, Org>(orgDto);
            org.Id =(Guid)new SequentialIdentityGenerator().Next;

            if (orgDto.ParentId != null && orgDto.ParentId != Guid.Empty)
            {
                org.Parent = _orgRepository.Get(o => o.Id == orgDto.ParentId);
            }
            _orgRepository.Add(org.AddOrgParentId(org));
            this.Context.Commit();
            return org.Id;

        }
        public void UpdateOrg(OrgDto orgDto)
        {
            var org = Mapper.Map<OrgDto, Org>(orgDto);
            _orgRepository.Update(org);
            this.Context.Commit();
        }

        public void DeleteOrg(Guid orgId)
        {
            _orgRepository.Remove(o => o.Id == orgId);
            this.Context.Commit();
        }

        public OrgDto GetOrg(Guid orgId)
        {
           var org= _orgRepository.Get(o => o.Id == orgId);
           var orgDto = Mapper.Map<Org, OrgDto>(org);
           return orgDto;
        }
        public OrgDto GetOrg(string orgCode)
        {
            var org = _orgRepository.Get(o => o.Code == orgCode);
            var orgDto = Mapper.Map<Org, OrgDto>(org);
            return orgDto;
        }
        public Dictionary<OrgDto, StorehouseDto> GetOrgAndDefaultWareHourse(string orgCode)
        {
            var org = _orgRepository.Get(o => o.Code == orgCode.Trim());
            var wareHouses = org.Storehouses ?? new HashSet<Storehouse>();
            var wh = wareHouses.FirstOrDefault(t => t.IsDefultStoehouuse == 1) ?? wareHouses.FirstOrDefault();
            var sh = Mapper.Map<Storehouse, StorehouseDto>(wh);
            var orgDto = Mapper.Map<Org, OrgDto>(org);
            var dirs = new Dictionary<OrgDto, StorehouseDto>();
            dirs.Add(orgDto, sh);
            return dirs;
        }
        public IList<OrgDto> GetAllOrg()
        {
           var orglst= _orgRepository.GetAll();
           return Mapper.Map<IEnumerable<Org>, IList<OrgDto>>(orglst);
        }

        public IList<OrgDto> GetAllOrg(Guid parentId)
        {
            var orglst = _orgRepository.GetAll(o=>o.ParentID==parentId);
            return Mapper.Map<IEnumerable<Org>, IList<OrgDto>>(orglst);
        }


        public Guid AddDept(DeptDto deptDto)
        {
            var dept = Mapper.Map<DeptDto, Dept>(deptDto);
            dept.Id = (Guid)new SequentialIdentityGenerator().Next;

            if (dept.ParentID != null && dept.ParentID != Guid.Empty)
            {
                dept.Parent = _deptRepository.Get(o => o.Id == deptDto.ParentId);
            }
            this._deptRepository.Add(dept.InitDept(dept));
            this.Context.Commit();
            return dept.Id;
        }
        public void UpdateDept(DeptDto deptDto)
        {
            var dept = Mapper.Map<DeptDto, Dept>(deptDto);
            dept.InitDept(dept);
            this._deptRepository.Update(dept);
            this.Context.Commit();
        }
        public void DeleteDept(Guid deptId)
        {
            this._deptRepository.Remove(d => d.Id == deptId);
            this.Context.Commit();
        }

        public OrgDto GetOrgByDeptId(Guid parentDeptId)
        {
            var orgId = _deptRepository.Get(d => d.ParentID == parentDeptId).OrgID;
            return this.GetOrg(orgId);
        }
        public IList<DeptDto> GetAllDept()
        {
            var deptlst = _deptRepository.GetAll();
            return Mapper.Map<IEnumerable<Dept>, IList<DeptDto>>(deptlst);
        }


        public DeptDto GetDept(Guid deptId)
        {
            var dept= _deptRepository.Get(d => d.Id == deptId);
            var deptDto = Mapper.Map<Dept, DeptDto>(dept);
            return deptDto;
        }
      
       
    }
}
